package com.huazai.controller;

import com.huazai.bean.po.User;
import com.huazai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.huazai.service.impl.UserServiceImpl.*;

/**
 * @author EZTTU
 */
@Controller
@RequestMapping("/door")
public class DoorController {
    private static final String LOGIN_FREE = "true";
    private UserService userService;

    @Autowired
    @Qualifier("UserServiceImpl")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping("/welcome")
    public String welcome(HttpServletRequest request, Model model) {
        Cookie[] cookies = request.getCookies();
        String email = null;
        String password = null;

        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("email".equals(name)) {
                email = cookie.getValue();
            } else if ("password".equals(name)) {
                password = cookie.getValue();
            }
        }

        if (email != null && password != null) {
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);
            String result = userService.validateLogin(user);

            if (result.contains(LOGIN_SUCCESS)) {
                String authority = result.substring(result.indexOf(":") + 1);
                user.setAuthority(authority);
                HttpSession session = request.getSession();
                session.setAttribute("userMessage", user);
                model.addAttribute("result", LOGIN_SUCCESS);
                return "redirect:/book/query";
            }

        }
        return "redirect:/door/toLoginPaper";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        String contextPath = request.getContextPath();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("email".equals(name) || "password".equals(name)) {
                cookie.setPath(contextPath);
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
        session.removeAttribute("userMessage");
        return "redirect:/door/toLoginPaper";
    }

    @RequestMapping("/toLoginPaper")
    public String toLoginPaper() {
        return "user/login";
    }

    @RequestMapping("/login")
    public String loginValidate(@RequestParam("email") String email,
                                @RequestParam("password") String password,
                                @RequestParam(value = "loginFree", required = false) String loginFree,
                                HttpServletRequest request,
                                HttpServletResponse response,
                                Model model) {

        User user = new User();

        user.setEmail(email);
        user.setPassword(password);

        String result = userService.validateLogin(user);

        if (result.contains(LOGIN_SUCCESS)) {
            String authority = result.substring(result.indexOf(":") + 1);
            result = result.substring(0, result.indexOf(":"));
            user.setAuthority(authority);
        }

        switch (result) {
            case LOGIN_SUCCESS:
                //正常登录后，用户的信息应该存在session里面。
                HttpSession session = request.getSession();
                session.setAttribute("userMessage", user);
                model.addAttribute("result", LOGIN_SUCCESS);

                if (LOGIN_FREE.equals(loginFree)) {
                    String contextPath = request.getContextPath();
                    Cookie accountCookie = new Cookie("email", email);
                    Cookie passwordCookie = new Cookie("password", password);

                    accountCookie.setPath(contextPath);
                    passwordCookie.setPath(contextPath);

                    accountCookie.setMaxAge(60 * 60 * 24 * 10);
                    passwordCookie.setMaxAge(60 * 60 * 24 * 10);

                    response.addCookie(accountCookie);
                    response.addCookie(passwordCookie);
                }
                return "redirect:/book/query";

            case USER_NO_EXIST:
                model.addAttribute("result", USER_NO_EXIST);
                return "user/message";

            case USER_NO_ACTIVE:
                model.addAttribute("result", USER_NO_ACTIVE);
                return "user/message";

            case PASSWORD_ERROR:
                model.addAttribute("result", PASSWORD_ERROR);
                return "user/message";

            default:
                return "redirect:/door/toLoginPaper";
        }

    }

}
